/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import java.util.Date;
import java.util.Calendar;

/**
 *
 * @author Kvest
 */
public class DateUtils
{
    public static String getCurrentDate()
    {
        Calendar calendar = getCalendar(null);

        String result = (calendar.get(Calendar.DATE) < 10 ? "0" : "") + calendar.get(Calendar.DATE) + "." +
                        ((calendar.get(Calendar.MONTH) + 1) < 10 ? "0" : "") + (calendar.get(Calendar.MONTH) + 1) + "." +
                        calendar.get(Calendar.YEAR);

        return result;
    }

    public static int getCurrentHour()
    {
         Calendar calendar = getCalendar(null);

         return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static int getCurrentMinute()
    {
         Calendar calendar = getCalendar(null);

         return calendar.get(Calendar.MINUTE);
    }

    public static int getCurrentMonth()
    {
         Calendar calendar = getCalendar(null);

         return (calendar.get(Calendar.MONTH) + 1);
    }

    public static int getCurrentDay()
    {
         Calendar calendar = getCalendar(null);

         return calendar.get(Calendar.DATE);
    }

    public static int getCurrentYear()
    {
         Calendar calendar = getCalendar(null);

         return calendar.get(Calendar.YEAR);
    }

     public static int compareDates (int day1,int month1,int year1,int day2,int month2,int year2)
     {
        if (year1 < year2)
        {
            return -1;
        }
        else if (year1 > year2)
        {
            return 1;
        }
        else
        {
            if (month1 < month2)
            {
                return -1;
            }
            else if (month1 > month2)
            {
                return 1;
            }
            else
            {
                if (day1 < day2)
                {
                    return -1;
                }
                else if (day1 > day2)
                {
                    return 1;
                }
                return 0;
            }
        }
    }

    private static int getDaysDifference(int day1, int month1, int day2, int month2, int year)
    {
        int count;
        if (month1 == month2)
        {
            count = day2 - day1;
        }
        else
        {
            //count = getMonthDays(month1,year) - day1 + 1;
            count = getMonthDays(month1,year) - day1;
            for (int i = month1 + 1; i < month2; i++)
            {
                count += getMonthDays(i,year);
            }
            //count += (day2 - 1);
            count += day2;
        }

        return count;
    }

    public static long getTicks(final String date)
    {
        long days = getDaysDifference("01.01.1970", date);

        return (days * 24L * 60L * 60L * 1000L);
    }


    public static int getDaysDifference(final String from, final String to)
    {
        int day1 = 0, month1 = 0, year1 = 0;

        try
        {
            day1 = Integer.parseInt(from.substring(0,from.indexOf(".")));
            month1 = Integer.parseInt(from.substring(from.indexOf(".") + 1,from.lastIndexOf('.')));
            year1 = Integer.parseInt(from.substring(from.lastIndexOf('.') + 1));
        }catch(Exception ex){}

        int day2 = 0, month2 = 0, year2 = 0;

        try
        {
            day2 = Integer.parseInt(to.substring(0,to.indexOf(".")));
            month2 = Integer.parseInt(to.substring(to.indexOf(".") + 1,to.lastIndexOf('.')));
            year2 = Integer.parseInt(to.substring(to.lastIndexOf('.') + 1));
        }catch(Exception ex){}

        int  count = 0;

        if (year1 == year2)
        {
            count = getDaysDifference(day1, month1, day2, month2, year1);
        }
        else
        {
            count = getDaysDifference(day1, month1, 31, 11, year1)+1;
            for (int i = year1+1; i < year2; i++)
            {
                count += 365;
                if (isLeapYear(i))
                {
                    count++;
                }
            }
            count += getDaysDifference(1,0,day2,month2,year2);
        }

        return count;
    }

    public static int getDaysDifference(final String date)
    {
        int day2 = 0, month2 = 0, year2 = getCurrentYear();

        try
        {
            day2 = Integer.parseInt(date.substring(0,date.indexOf(".")));
            month2 = Integer.parseInt(date.substring(date.indexOf(".") + 1,date.lastIndexOf('.')));
            year2 = Integer.parseInt(date.substring(date.lastIndexOf('.') + 1));
        }catch(Exception ex){}

        int day1 = getCurrentDay();
        int month1 = getCurrentMonth();
        int year1 = getCurrentYear();

        int tmp, koeff = 1, count = 0;
        if (compareDates(day1, month1, year1, day2, month2, year2) > 0)
        {
            tmp = day1; day1 = day2; day2 = tmp;
            tmp = month1; month1 = month2; month2 = tmp;
            tmp = year1; year1 = year2; year2 = tmp;
            koeff = -1;
        }

        if (year1 == year2)
        {
            count = getDaysDifference(day1, month1, day2, month2, year1);
        }
        else
        {
            count = getDaysDifference(day1, month1, 31, 11, year1)+1;
            for (int i = year1+1; i < year2; i++)
            {
                count += 365;
                if (isLeapYear(i))
                {
                    count++;
                }
            }
            count += getDaysDifference(1,0,day2,month2,year2);
        }
        
        return koeff * count;
    }

    public static boolean isLeapYear (int year)
    {
        return ((year%4==0) && (year%100!=0) || (year%400==0));
    }

    private static int getMonthDays(int month , int year)
    {
        switch(month)
        {
            case 1 : return 31;
            case 2:
                    if (isLeapYear(year))
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
            case 3 : return 31;
            case 4 : return 30;
            case 5 : return 31;
            case 6 : return 30;
            case 7 : return 31;
            case 8 : return 31;
            case 9 : return 30;
            case 10 : return 31;
            case 11 : return 30;
            case 12 : return 31;
        }

        //Условие недостижимое, но Java требует что нибудь вернуть
        return 30;
    }

    public static String calculateDate(final String currentDate,final int deltaDays)
    {
        int day = 0, month = 0, year = 0;

        try
        {
            day = Integer.parseInt(currentDate.substring(0,currentDate.indexOf(".")));
            month = Integer.parseInt(currentDate.substring(currentDate.indexOf(".") + 1,currentDate.lastIndexOf('.')));
            year = Integer.parseInt(currentDate.substring(currentDate.lastIndexOf('.') + 1));
        }catch(Exception ex){}

        if (deltaDays >= 0)
        {
            for (int i = 0; i < deltaDays; ++i)
            {
                day++;
                if (day > getMonthDays(month , year))
                {
                    day = 1;
                    month++;
                    if (month > 12)
                    {
                        month = 1;
                        year++;
                    }
                }
            }
        }
        else
        {
            for (int i = deltaDays; i < 0; i++)
            {
                day--;
                if (day < 1)
                {
                    month--;
                    if (month < 1)
                    {
                        month = 12;
                        year--;
                    }
                    day = getMonthDays(month , year);
                }
            }
        }

        return (day < 10 ? "0" : "") + day + "." + (month < 10 ? "0" : "") + month + "." + year;
    }

    private static Calendar getCalendar(String date)
    {
        Date d = new Date(System.currentTimeMillis());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
 
        if (date != null)
        {
            try
            {
                calendar.set(Calendar.DATE, Integer.parseInt(date.substring(0,date.indexOf("."))));  
                calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(date.indexOf(".") + 1,date.lastIndexOf('.'))));
                calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(date.lastIndexOf('.') + 1)));
            }catch(Exception ex){}
        }

        return calendar;
    }

    public static String getDayOfWeek(String date)
    {
        Calendar calendar = getCalendar(date);

        int day = calendar.get(Calendar.DATE);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);

        int a = (14 - month)/12;
        int y = year - a;
        int m = month + 12*a - 2;
        int week = (day+y+(y/4)-(y/100)+(y/400)+(31*m)/12) % 7;

        switch (week)
        {
            case 0 : return "sun";
            case 1 : return "mnd";
            case 2 : return "tsd";
            case 3 : return "wnd";
            case 4 : return "thd";
            case 5 : return "frd";
            case 6 : return "std";
            default: return "";
        }
    }
}
